﻿using UnityEngine;
using System.Collections;
using GUIElements;

namespace PlayerClasses
{

	/// <summary>
	/// Squad.
	/// </summary>
	public class Squad
	{
		private Character[] characters;
		private int currentMembers;
		private int level;
		private static Squad m_instance;
		public Squad ()
		{
			m_instance = this;
			level = 0;
			currentMembers = 4;
			//TODO: create character creation class
			characters = new Character[currentMembers];

			//TODO: Update this with a game ID to check for current game (allows for multiple game saves)
			//TODO:Listener for leaving battlescene, make a copy of the tVars, delete Tvars after all loads, delete cVars once loaded in overworld, protects against crashes

			if(PlayerPrefs.HasKey("Tcharacter0Name"))  //check to see if a game has been run this session by checking the transfer variables
			{
				for(int i = 0; i< characters.Length; i++)
				{
					characters[i].Name = PlayerPrefs.GetString("Tcharacter"+i+"name:");
					characters[i].Level = PlayerPrefs.GetInt("Tcharacter"+i+"level:");
					characters[i].Health = PlayerPrefs.GetInt("Tcharacter"+i+"health:");
					characters[i].Armour = PlayerPrefs.GetInt("Tcharacter"+i+"armour:");
					characters[i].Agility = PlayerPrefs.GetInt("Tcharacter"+i+"agility:");
					characters[i].Stamina = PlayerPrefs.GetInt("Tcharacter"+i+"stamina:");
					characters[i].Speed = PlayerPrefs.GetInt("Tcharacter"+i+"speed:");
					characters[i].Strength = PlayerPrefs.GetInt("Tcharacter"+i+"strength:");
					characters[i].WeaponProficiency = PlayerPrefs.GetInt("Tcharacter"+i+"weaponProf:");
					characters[i].TdmgProficiency = PlayerPrefs.GetInt("Tcharacter"+i+"TdmgProf:");

					//clear transfer variabls after load

					PlayerPrefs.DeleteKey("Tcharacter"+i+"name:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"level:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"health:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"armour:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"agility:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"stamina:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"speed:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"strength:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"weaponProf:");
					PlayerPrefs.DeleteKey("Tcharacter"+i+"TdmgProf:");

					//clear cvars if overworld
					if(Application.loadedLevelName == "OverWorld"){ //clear crash variables if you load into overworld as you dont need them now
						PlayerPrefs.DeleteKey("Ccharacter"+i+"name:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"level:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"health:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"armour:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"agility:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"stamina:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"speed:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"strength:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"weaponProf:");
						PlayerPrefs.DeleteKey("Ccharacter"+i+"TdmgProf:");
					}
				}

			}
			else if(PlayerPrefs.HasKey("Ccharacter0name"))//check to see if a game has crashed during a level, if so then load from these vars, these vars wont exist if a game has succesfuly made it back to overworld
			{
				for(int i=0; i< characters.Length; i++)
				{
					//load from crash variables
					characters[i].Name = PlayerPrefs.GetString("Ccharacter"+i+"name:");
					characters[i].Level = PlayerPrefs.GetInt("Ccharacter"+i+"level:");
					characters[i].Health = PlayerPrefs.GetInt("Ccharacter"+i+"health:");
					characters[i].Armour = PlayerPrefs.GetInt("Ccharacter"+i+"armour:");
					characters[i].Agility = PlayerPrefs.GetInt("Ccharacter"+i+"agility:");
					characters[i].Stamina = PlayerPrefs.GetInt("Ccharacter"+i+"stamina:");
					characters[i].Speed = PlayerPrefs.GetInt("Ccharacter"+i+"speed:");
					characters[i].Strength = PlayerPrefs.GetInt("Ccharacter"+i+"strength:");
					characters[i].WeaponProficiency = PlayerPrefs.GetInt("Ccharacter"+i+"weaponProf:");
					characters[i].TdmgProficiency = PlayerPrefs.GetInt("Ccharacter"+i+"TdmgProf:");

					//delete crash variable after load
					PlayerPrefs.DeleteKey("Ccharacter"+i+"name:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"level:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"health:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"armour:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"agility:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"stamina:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"speed:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"strength:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"weaponProf:");
					PlayerPrefs.DeleteKey("Ccharacter"+i+"TdmgProf:");

				}
			}else{
				if(PlayerPrefs.HasKey ("Scharacter0Name")) //if not check if there is a save file from a previous run time against the save variables
				{
					for(int i =0; i <characters.Length; i++)
					{
						characters[i].Name = PlayerPrefs.GetString("Scharacter"+i+"name:");
						characters[i].Level = PlayerPrefs.GetInt("Scharacter"+i+"level:");
						characters[i].Health = PlayerPrefs.GetInt("Scharacter"+i+"health:");
						characters[i].Armour = PlayerPrefs.GetInt("Scharacter"+i+"armour:");
						characters[i].Agility = PlayerPrefs.GetInt("Scharacter"+i+"agility:");
						characters[i].Stamina = PlayerPrefs.GetInt("Scharacter"+i+"stamine=a:");
						characters[i].Speed = PlayerPrefs.GetInt("Scharacter"+i+"speed:");
						characters[i].Strength = PlayerPrefs.GetInt("Scharacter"+i+"strength:");
						characters[i].WeaponProficiency = PlayerPrefs.GetInt("Scharacter"+i+"weaponProf:");
						characters[i].TdmgProficiency = PlayerPrefs.GetInt("Scharacter"+i+"TdmgProf:");
					}
				}
				else{ //if none of these exist, create a new game
					Debug.Log ("created new");
					for (int i = 0; i < characters.Length; i++) {
						characters [i] = new Character ("Meow");
					}
				}
			}
		}

		void onDestroy()
		{
		
		}
		/// <summary>
		/// Saves the Squad.
		/// 0 = transfering scenes
		/// 1 = manual save button
		/// </summary>
		/// <param name="type">Type.</param>
		public void SquadSave(int type) //0=Transfer 1=save
		{
			Debug.Log ("saved");

			for(int i=0; i<characters.Length; i++)
			{
				if(type == 0){
					//create transfer variables
					PlayerPrefs.SetString("Tcharacter"+i+"name:",characters[i].Name);
					string test = "saved character"+i+"name:"+characters[i].Name;
					Debug.Log(test);
					PlayerPrefs.SetInt("Tcharacter"+i+"level:",characters[i].Level);
					PlayerPrefs.SetInt("Tcharacter"+i+"health:",characters[i].Health);
					PlayerPrefs.SetInt("Tcharacter"+i+"armour:",characters[i].Armour);
					PlayerPrefs.SetInt("Tcharacter"+i+"agility:",characters[i].Agility);
					PlayerPrefs.SetInt("Tcharacter"+i+"stamina:",characters[i].Stamina);
					PlayerPrefs.SetInt("Tcharacter"+i+"speed:",characters[i].Speed);
					PlayerPrefs.SetInt("Tcharacter"+i+"strength:",characters[i].Strength);
					PlayerPrefs.SetInt("Tcharacter"+i+"weaponProf:",characters[i].WeaponProficiency);
					PlayerPrefs.SetInt("Tcharacter"+i+"TdmgProf:",characters[i].TdmgProficiency);

					//create crash variables
					if(Application.loadedLevelName == "OverWorld") //create crash variables on transfer to non overworld
					{
						PlayerPrefs.SetString("Ccharacter"+i+"name:",characters[i].Name);
						PlayerPrefs.SetInt("Ccharacter"+i+"level:",characters[i].Level);
						PlayerPrefs.SetInt("Ccharacter"+i+"health:",characters[i].Health);
						PlayerPrefs.SetInt("Ccharacter"+i+"armour:",characters[i].Armour);
						PlayerPrefs.SetInt("Ccharacter"+i+"agility:",characters[i].Agility);
						PlayerPrefs.SetInt("Ccharacter"+i+"stamina:",characters[i].Stamina);
						PlayerPrefs.SetInt("Ccharacter"+i+"speed:",characters[i].Speed);
						PlayerPrefs.SetInt("Ccharacter"+i+"strength:",characters[i].Strength);
						PlayerPrefs.SetInt("Ccharacter"+i+"weaponProf:",characters[i].WeaponProficiency);
						PlayerPrefs.SetInt("Ccharacter"+i+"TdmgProf:",characters[i].TdmgProficiency);
					}

				}else if(type ==1){

					PlayerPrefs.SetString("Scharacter"+i+"name:",characters[i].Name);
					string test = "saved character"+i+"name:"+characters[i].Name;
					Debug.Log(test);
					PlayerPrefs.SetInt("Scharacter"+i+"level:",characters[i].Level);
					PlayerPrefs.SetInt("Scharacter"+i+"health:",characters[i].Health);
					PlayerPrefs.SetInt("Scharacter"+i+"armour:",characters[i].Armour);
					PlayerPrefs.SetInt("Scharacter"+i+"agility:",characters[i].Agility);
					PlayerPrefs.SetInt("Scharacter"+i+"stamina:",characters[i].Stamina);
					PlayerPrefs.SetInt("Scharacter"+i+"speed:",characters[i].Speed);
					PlayerPrefs.SetInt("Scharacter"+i+"strength:",characters[i].Strength);
					PlayerPrefs.SetInt("Scharacter"+i+"weaponProf:",characters[i].WeaponProficiency);
					PlayerPrefs.SetInt("Scharacter"+i+"TdmgProf:",characters[i].TdmgProficiency);
				}
			}
		}

		/// <summary>
		/// Loads Squad from Saved vars in PlayerPrefs
		/// </summary>
		public void SquadLoad(){
			for(int i = 0; i< characters.Length; i++)
			{
			//load from saved variables
			characters[i].Name = PlayerPrefs.GetString("Scharacter"+i+"name:");
			characters[i].Level = PlayerPrefs.GetInt("Scharacter"+i+"level:");
			characters[i].Health = PlayerPrefs.GetInt("Scharacter"+i+"health:");
			characters[i].Armour = PlayerPrefs.GetInt("Scharacter"+i+"armour:");
			characters[i].Agility = PlayerPrefs.GetInt("Scharacter"+i+"agility:");
			characters[i].Stamina = PlayerPrefs.GetInt("Scharacter"+i+"stamine=a:");
			characters[i].Speed = PlayerPrefs.GetInt("Scharacter"+i+"speed:");
			characters[i].Strength = PlayerPrefs.GetInt("Scharacter"+i+"strength:");
			characters[i].WeaponProficiency = PlayerPrefs.GetInt("Scharacter"+i+"weaponProf:");
			characters[i].TdmgProficiency = PlayerPrefs.GetInt("Scharacter"+i+"TdmgProf:");
			}
		}

		/// <summary>
		/// Gets or sets the <see cref="PlayerClasses.Squad"/> at the specified index.
		/// </summary>
		/// <param name="index">Index.</param>
		public Character this [int index] {
			get {
				return characters [index];
			}
			set {
				characters [index] = value;
			}
		}

		/// <summary>
		/// Gets or sets the current members.
		/// </summary>
		/// <value>The current members.</value>
		public int CurrentMembers {
			get {
				return currentMembers;
			}
			set {
				currentMembers = value;
			}
		}

		/// <summary>
		/// Gets or sets the level.
		/// </summary>
		/// <value>The level.</value>
		public int Level {
			get {
				return level;
			}
			set {
				level = value;
			}
		}

		/// <summary>
		/// Gets the instance.
		/// </summary>
		/// <value>The instance.</value>
		public static Squad Instance {
			get {
				if (m_instance == null) {
					Debug.LogError ("squad manager: No instance of this object found!");
				}
				return m_instance;
			}
		}
	}
}
